package com.txsj.scan;

import android.app.Activity;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import com.tencent.bugly.crashreport.CrashReport;
import com.txsj.scan.reader.base.ERROR;
import com.txsj.scan.reader.helper.ReaderHelper;
import com.txsj.scan.reader.tools.Beeper;
import com.txsj.scan.test.ReadSignActivity;

import java.io.IOException;
import java.net.Socket;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;


/**
 * 应用上下文
 */

public class MyApplication extends Application {
    private static MyApplication instance;

    private Socket mTcpSocket = null;
    private BluetoothSocket mBtSocket = null;
    public ArrayList<CharSequence> mMonitorListItem = new ArrayList<CharSequence>();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        try {
            ReaderHelper.setContext(getApplicationContext());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Beeper.init(instance);

        //腾讯云bugly初始化
        CrashReport.initCrashReport(getApplicationContext(), "7f8fefb2c1", false);
    }

    public static MyApplication getInstance() {
        return instance;
    }


    public final void writeMonitor(String strLog, int type) {
        Date now = new Date();
        SimpleDateFormat temp = new SimpleDateFormat("kk:mm:ss");
        SpannableString tSS = new SpannableString(temp.format(now) + ":\n"
                + strLog);
        tSS.setSpan(new ForegroundColorSpan(type == ERROR.SUCCESS ? Color.BLACK
                        : Color.RED), 0, tSS.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mMonitorListItem.add(tSS);
        if (mMonitorListItem.size() > 1000)
            mMonitorListItem.remove(0);
    }

    private List<Activity> activities = new ArrayList<Activity>();

    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        //ControlGPIO.newInstance().JNIwriteGPIO(0);
        for (Activity activity : activities) {
            try {
                activity.finish();
            } catch (Exception e) {
                ;
            }
        }

        try {
            if (mTcpSocket != null)
                mTcpSocket.close();
            if (mBtSocket != null)
                mBtSocket.close();
        } catch (IOException e) {
        }

        mTcpSocket = null;
        mBtSocket = null;
        if (ReadSignActivity.mSerialPort != null) {
            Log.e("close serial", "serial");
            ReadSignActivity.mSerialPort.close();
        }

        if (BluetoothAdapter.getDefaultAdapter() != null)
            BluetoothAdapter.getDefaultAdapter().disable();

        System.exit(0);
    }


}
